﻿using System;
using TIS.DAO;
using System.Data;
using System.Web.UI.WebControls;

namespace TIS.Home
{
    public partial class Default : System.Web.UI.Page
    {
        News news = new News();
        NewsDAO _newsDao = new NewsDAO();
        CategoryDAO _categoryDao = new CategoryDAO();
        SettingDAO _settingDao = new SettingDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                if (Session["lang"] == null)
                {
                    Session["lang"] = 1;
                }
                Introduct();
                Setting();
                BindHoatDong();
                DataTable dt = _categoryDao.GetMenus(int.Parse(Session["lang"].ToString()));
                rptMenus.DataSource = dt;
                rptMenus.DataBind();

                rptBoxMenu.DataSource = dt;
                rptBoxMenu.DataBind();
            }
        }

        public void Setting()
        {

            ltlMetaTitle.Text = _settingDao.GetValue("title", int.Parse(Session["lang"].ToString()));
            ltlMetaDescription.Text = _settingDao.GetValue("description", int.Parse(Session["lang"].ToString()));
            ltlMetaKeywords.Text = _settingDao.GetValue("keywords", int.Parse(Session["lang"].ToString()));
        }
        public void Introduct()
        {
            news.NewsId = 1;
            var table = news.SelectOne();
            if (table != null)
            {
                rpIntroduction.DataSource = table;
                rpIntroduction.DataBind();
            }
        }

  
        public void BindHoatDong()
        {
            rpTinKhac.DataSource = _newsDao.GetTinKhac(int.Parse(Session["lang"].ToString()));
            rpTinKhac.DataBind();
            rpHoatDong.DataSource = _newsDao.GetTinHoatDong(int.Parse(Session["lang"].ToString()));
            rpHoatDong.DataBind();
        }

        protected void rptBoxMenu_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            DataRowView dv = (DataRowView)e.Item.DataItem;
          
            if (dv != null)
            {
               int categoryId = int.Parse(dv.Row["CategoryId"].ToString());
             
               if (e.Item.ItemIndex == 0)
               {
     
                   Repeater newBoxLeft = (Repeater)e.Item.FindControl("rptBoxSubMenu0");
                   newBoxLeft.DataSource = _newsDao.GetService(categoryId, int.Parse(Session["lang"].ToString()));
                   newBoxLeft.DataBind();

               }
               if (e.Item.ItemIndex == 1)
               {
                   Repeater newBoxLeft1 = (Repeater)e.Item.FindControl("rptBoxSubMenu1");
                   newBoxLeft1.DataSource = _newsDao.GetService(categoryId, int.Parse(Session["lang"].ToString()));
                   newBoxLeft1.DataBind();
               }

               if (e.Item.ItemIndex == 2)
               {
                   Repeater newBoxLeft2 = (Repeater)e.Item.FindControl("rptBoxSubMenu2");
                   newBoxLeft2.DataSource = _newsDao.GetService(categoryId, int.Parse(Session["lang"].ToString()));
                   newBoxLeft2.DataBind();
               }

            }
        }

        
    }
}